package com.arsl.client.view;

import java.util.Date;

import org.apache.http.conn.ssl.StrictHostnameVerifier;

import com.arsl.client.GlobalInfo;
import com.arsl.client.PaperaziServiceAsync;
import com.arsl.client.data.Paper;
import com.arsl.client.event.KillContainerEvent;
import com.arsl.client.event.ShowViewEvent;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class PageSelectorPanel extends Composite {

	HandlerManager eventBus;
	PaperaziServiceAsync rpcService;

	private HasWidgets container;

	private static PageSelectorPanelUiBinder uiBinder = GWT
			.create(PageSelectorPanelUiBinder.class);

	interface PageSelectorPanelUiBinder extends
			UiBinder<Widget, PageSelectorPanel> {
	}

	public PageSelectorPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		
	}

	public void setEventBus(HandlerManager eventBus) {
		this.eventBus = eventBus;
	}

	public void setContainer(HasWidgets container) {
		this.container = container;
	}

	public void setRpcService(PaperaziServiceAsync rpcService) {
		this.rpcService = rpcService;
		pageSelector.setRpcService(rpcService);
		pageSelector.populatePapers();
	}

	@UiField
	Button btnOk;

	@UiField
	Button btnCancel;

	@UiField
	PageSelectorWidget pageSelector;

	@UiHandler("btnCancel")
	public void cancelClickHandler(ClickEvent event) {
		eventBus.fireEvent(new KillContainerEvent(container));

	}

	@UiHandler("btnOk")
	public void okClickHandler(ClickEvent event) {
		if (pageSelector.isValid()) {
			final int paperId = pageSelector.getPaperId();
			Date date = pageSelector.getDate();
			long t = date.getTime();
			java.sql.Date dt = new java.sql.Date(t);
			int editionId = pageSelector.getEditionId();
			final int sectionId = pageSelector.getSectionId();
			final int pageNo = pageSelector.getPageNo();
			rpcService.addPublication(paperId, dt, editionId,
					new AsyncCallback<Integer>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(Integer publicationId) {
							rpcService.addARPage(publicationId, sectionId,
									pageNo, new AsyncCallback<Integer>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method
											// stub

										}

										@Override
										public void onSuccess(Integer arPageId) {
											ShowViewEvent event = new ShowViewEvent();
											event.setArg(
													ShowViewEvent.VIEW_ARG,
													ShowViewEvent.VIEW_PAGE_TAGGER);
											event.setArg(
													ShowViewEvent.ARG_ARPAGEID,
													arPageId + "");
											event.setArg(
													ShowViewEvent.ARG_PAPERID,
													paperId + "");
											eventBus.fireEvent(event);
											eventBus.fireEvent(new KillContainerEvent(
													container));
										}
									});
						}
					});

		} else {
			Window.alert("Incorrect parameters for the page selection");
		}

	}

}
